package com.hardroid.workload.workcalc;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

import android.util.Log;

public class ObsvResult implements Serializable {

	public static final int STATUS_DATA_ENOUGH = 1;
	public static final int STATUS_DATA_NOT_ENOUGH = 1;

	public Employer employer;

	public boolean status;
	public int confidenceLevel;
	public int accuracyLevel;
	
	public double pBarTotal= 0;
	

	public ArrayList<ObsvResultItem> lObsvDataItem;

	public ObsvResult() {
		// TODO Auto-generated constructor stub
		lObsvDataItem = new ArrayList<ObsvResultItem>();
	}

	public ObsvResult(Employer employer) {
		// TODO Auto-generated constructor stub
		lObsvDataItem = new ArrayList<ObsvResultItem>();
		this.employer = employer;
	}

	public void generateResult() {
		lObsvDataItem.clear();
		confidenceLevel = 0;
		accuracyLevel = 0;
		status = true;
		int n = 0;
		// try {
		for (ObsvDay day : employer.sheet.lObsvDay) {
//			Log.i("Generate result ", (n + 1) + "");
			ObsvResultItem res = new ObsvResultItem();
			res.obsvDate = new Date(day.obsvDate.getTime());
			res.tProductive = day.getTotalProductive();
			res.tNProductive = day.getTotalNonProductive();
			res.pProductive = day.getPercentageProductive();
			res.cProductive = employer.sheet.getCumProductive(n);
			res.cPProductive = employer.sheet.getCumPercentageProductive(n);
			pBarTotal += (double)res.pProductive/100;
			n++;
			lObsvDataItem.add(res);
		}
		pBarTotal = (double)pBarTotal / n;
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
	}
	
	/**
	 * Mengecek apakah observasi hari ini valid atau tidak
	 * 
	 * @param n jumlah hari
	 * @param cumm cummulative persen produktif
	 * @param prod produktifitas saat itu / persen produktif
	 * @return
	 */
	public boolean getValidity( int prod){
		double batasBawah , batasAtas, pbar;				
		// %productivitas			
		double persenProd = (double)prod / 100;
		
		// pbar = total persen produktif 
		batasBawah = pBarTotal - 3 * (Math.sqrt((pBarTotal * (1 - pBarTotal)/lObsvDataItem.size())));
		batasAtas= pBarTotal + 3 * (Math.sqrt((pBarTotal * (1 - pBarTotal)/lObsvDataItem.size())));
//		Log.i("Obsv Day Get Validity","pbar="+ pBarTotal+",batas="+ batasBawah+"-"+batasAtas+",prod="+persenProd);	
		return  persenProd>= batasBawah && persenProd<= batasAtas;	
	}

	public boolean checkEnoughData() {
		// TODO Auto-generated method stub
		int nday = employer.sheet.lObsvDay.size();
		int ntally = employer.sheet.lObsvDay.get(0).lTally.size();
		double nP = 0, n = 0;
		for (ObsvResultItem res : lObsvDataItem) {		
			res.validity = getValidity(res.pProductive);
			if (res.validity) {
				nP += (double)res.pProductive /100;
//				Log.i("Valid", nP+"");
				n++;
			}
		}		
		if (n > 0) {
			double pbar =(double) (nP) / n ;
			double N = 400* (1 - pbar) / pbar;
//			Log.i("RESULT - Check data enough","nP="+nP+ ",pbar="+pbar+",n="+n+",N="+N);
			if (N < ntally * nday) {
				status = true;				
			} else {
				status = false;				
			}
		}else{
			status = false;
			
		}
		return status;

	}

}
